package examples;

import static org.dinqy.Criteria.equal;
import static org.dinqy.Criteria.like;

import java.util.List;

import javax.persistence.EntityManager;

import org.dinqy.Dinqy;
import org.dinqy.integration.EntityManagerModule;
import org.dinqy.syntax.Finder;
import org.entity.Book;
import org.entity.DataFixtures;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;

/**
 * @author armandino (at) gmail.com
 */
public class UsageExamples {
    @Inject
    private EntityManager entityManager;
    
    /**
     * @see DataFixtures
     */
    public UsageExamples() {
        Injector injector = Guice.createInjector(new EntityManagerModule());
        injector.injectMembers(this);
        
        DataFixtures fixtures = new DataFixtures(entityManager);
        fixtures.initData();
    }
    
    @SuppressWarnings("unused")
    public void usageExamples() {
        Finder<Book> findBook = new Dinqy(entityManager).finder(Book.class);
        
        Book book;
        List<Book> bookList;
        
        // select book from Book where book.title = :title
        book = findBook.where("title", equal("Great Expectations")).result();
        
        // select book from Book where book.title like :title
        book = findBook.where("title", like("%Adventures%")).result();
        
        // select book from Book where book.title like :title
        // and book.author.name = :name
        book = findBook.where("title", like("%Oliver Twist"))
            .and("author.name", equal("Charles Dickens"))
            .result();
        
        // select book from Book where and book.author.name = :name limit 10
        bookList = findBook.where("author.name", equal("Albert Camus"))
            .asc("title")
            .limit(10)
            .list();
        
        //System.out.println(book);
        //System.out.println(bookList);
    }
    
    public static void main(String[] args) {
        UsageExamples usage = new UsageExamples();
        usage.usageExamples();
    }
}
